package com.qr.controller;

import com.qr.dao.IAccountDao;
import com.qr.service.IAccountService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 *  Spring核心容器
 *      ApplicationContext:应用上下文对象
 *          三大子类
 *          ClassPathXmlApplicationContext:
 *              加载类路径下的资源文件，类路径下的资源文件必须是在resource目录下
 *              具体目录还是需要来进行指定的
 *
 *          FileSystemXmlApplicationContext:
 *              加载磁盘路径下的配置
 *          AnnontationConfig:
 *              读取注解创建容器
 *      BeanFatory：工厂模式加载
 *          XmlBeanFactory(new ClassPathResources("xxxxx.xml"))
 *
 *      核心容器：
 *          ApplicationContext适用于单例对象，在创建对象的时候采用的是立即加载的方式
 *          BeanFatory适用于多例对象，创建对象的时候是使用延迟加载
 *
 */
public class AccountController {
    public static void main(String[] args) {
        //在resource目录下的mapper文件夹中
        ApplicationContext class1 = new ClassPathXmlApplicationContext("mapper/bean.xml");
        //直接放在resource目录下
//        ClassPathXmlApplicationContext class2 = new ClassPathXmlApplicationContext("bean.xml");
        System.out.println(class1);

        //通过bean中指定的id来进行获取
        IAccountService accountService = (IAccountService) class1.getBean("accountService");
        accountService.saveAccount();

//        读取资源文件中的
        FileSystemXmlApplicationContext class3 = new FileSystemXmlApplicationContext("D:\\bean.xml");
        IAccountDao accountDao = (IAccountDao) class3.getBean("accountDao");
        accountDao.saveAccount();

        /**
         * BeanFactory加载
         */
        System.out.println("核心容器中工厂加载");
        BeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("mapper/bean.xml"));
        IAccountDao accountDao1 = (IAccountDao) xmlBeanFactory.getBean("accountDao");
        accountDao1.saveAccount();

    }
}
